package team.group.exam.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.opensymphony.xwork2.ActionContext;

import team.group.exam.dao.IPaperDao;
import team.group.exam.entity.PaperCondition;
import team.group.exam.entity.TbPaper;
import team.group.exam.entity.TbPaperBank;
import team.group.exam.entity.TbQuestionbank;


public class PaperDaoImpl extends HibernateDaoSupport implements IPaperDao {
    
	@Override
	public void deletePaper(TbPaper paper) {
		// TODO Auto-generated method stub
          this.getHibernateTemplate().delete(paper);
	}

	@Override
	public List<TbPaper> getPaperByType(String type) {
		// TODO Auto-generated method stub
		 Object value[]={type.toString()};
		 return this.getHibernateTemplate().find("from TbPaper where paper_type=?",value);
	}

	@Override
	public TbPaper getById(int id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(TbPaper.class, id);
	}

	@Override
	public void addPaper(TbPaper paper) {
		// TODO Auto-generated method stub
	    this.getHibernateTemplate().save(paper);
	}

	@Override
	public void addPaperTitle(TbPaperBank paperBank) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().save(paperBank);
	}

	@Override
	public TbPaper getByPaper(TbPaper paper) {
		// TODO Auto-generated method stub
		List list = this.getHibernateTemplate().find("from TbPaper where paperName=?",paper.getPaperName().toString());
		return (TbPaper) list.get(0);
	}

	@Override
	public List<TbPaperBank> getTbPaperBankList(TbPaper paper,int currentPage,int pageSize) {
		// TODO Auto-generated method stub
		// return this.getHibernateTemplate().find("from TbPaperBank where tbPaper.paperId=?",paper.getPaperId());
		 Session session=this.getHibernateTemplate().getSessionFactory().openSession();
	     Criteria c=session.createCriteria(TbPaperBank.class);
	     List<TbPaperBank> list=c.add(Restrictions.eq("tbPaper.paperId", paper.getPaperId()))
	                       .setMaxResults(pageSize)
	                       .setFirstResult((currentPage-1)*pageSize)
	    		           .list();
	     Map<String,Object> map=ActionContext.getContext().getSession();
	     map.put("thelist", list);
	     session.close();              
	     return (List<TbPaperBank>) map.get("thelist");
	}
    /**
     * 按照类型进行查询
     */
	@Override
	public List<TbPaper> getPaperName(PaperCondition paperCondition) {
		// TODO Auto-generated method stub
		//return this.getHibernateTemplate().find("from TbPaper");
		if(paperCondition!=null){
		    System.out.println("Type类型："+paperCondition.getType());
		    }
		Session session=this.getHibernateTemplate().getSessionFactory().openSession();
		
		Criteria c=session.createCriteria(TbPaper.class);
		if(paperCondition!=null){
			if(paperCondition.getType()!=null){
				c.add(Restrictions.ilike("paperType",paperCondition.getType(),MatchMode.ANYWHERE));
			}
	   }
		List list1=c.list();
        session.close();
        return list1;
	}

	@Override
	public int getTotalPage(TbPaper paper,int pageSize) {
		List list=this.getHibernateTemplate().find("from TbPaperBank where tbPaper.paperId=?",paper.getPaperId());
		int totalSize=list.size();
		System.out.println("总页数"+totalSize);
		int totalpage=0;
		if(totalSize%pageSize==0){
			totalpage=totalSize/pageSize;
		}
		else{
			totalpage=totalSize/pageSize+1;
		}
		return totalpage;
	}

	@Override
	public List<TbPaperBank> getAllTiltle(TbPaper paper) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().find("from TbPaperBank where tbPaper.paperId=?",paper.getPaperId());
	}

	@Override
	public List<Integer> getAllRightAnswer(TbPaper paper) {
		// TODO Auto-generated method stub
		List list=this.getHibernateTemplate().find("select tbQuestionbank.titleId from TbPaperBank where tbPaper.paperId=?",paper.getPaperId());
		return list;	
	}
    /**
     * 将用户名和分数插入数据中
     */
	@Override
	public void addScoreAndUserId(TbPaper paper) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(paper);
		
	}

	@Override
	public List<String> getPaperType() {
		// TODO Auto-generated method stub
		List<String> list = this.getHibernateTemplate().find("select distinct paperType from TbPaper");
		return list;
	}
	
	
}
